home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / doprnt.c < prev    next >
C/C++ Source or Header  |  1993-10-06  |  6KB  |  197 lines

  1. /* Output like sprintf to a buffer of specified size.
  2.    Also takes args differently: pass one pointer to an array of strings
  3.    in addition to the format string which is separate.
  4.    Copyright (C) 1985 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Emacs.
  7.  
  8. GNU Emacs is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU Emacs is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU Emacs; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. #ifdef emacs
  27. #include "config.h"
  28. #include "lisp.h"
  29.  
  30. #ifdef STDC_HEADERS
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #endif
  34. #include "doprnt_p.h"
  35. #endif
  36.  
  37. /* Generate output from a format-spec FORMAT,
  38.    terminated at position FORMAT_END.
  39.    Output goes in BUFFER, which has room for BUFSIZE chars.
  40.    If the output does not fit, truncate it to fit.
  41.    Returns the number of characters stored into BUFFER.
  42.    ARGS points to the vector of arguments, and NARGS says how many.
  43.    A double counts as two arguments.  */
  44.  
  45. int
  46. doprnt (buffer, bufsize, format, format_end, nargs, args)
  47.      char *buffer;
  48.      register int bufsize;
  49.      char *format;
  50.      char *format_end;
  51.      int nargs;
  52.      char **args;
  53. {
  54.   int cnt = 0;            /* Number of arg to gobble next */
  55.   register char *fmt = format;    /* Pointer into format string */
  56.   register char *bufptr = buffer; /* Pointer into output buffer.. */
  57.   /* Use this for sprintf unless we need something really big.  */
  58.   char tembuf[100];
  59.   /* Size of sprintf_buffer.  */
  60.   int size_allocated = 100;
  61.   /* Buffer to use for sprintf.  Either tembuf or same as BIG_BUFFER.  */
  62.   char *sprintf_buffer = tembuf;
  63.   /* Buffer we have got with malloc.  */
  64.   char *big_buffer = 0;
  65.   register int tem;
  66.   char *string;
  67.   char fmtcpy[20];
  68.   int minlen;
  69.  
  70.   if (format_end == 0)
  71.     format_end = format + strlen (format);
  72.  
  73.   bufsize--;
  74.   while (fmt != format_end && bufsize > 0)    /* Loop until end of format
  75.                            string or buffer full */
  76.     {
  77.       if (*fmt == '%')    /* Check for a '%' character */
  78.     {
  79.       int size_bound;
  80.  
  81.       fmt++;
  82.       /* Copy this one %-spec into fmtcpy.  */
  83.       string = fmtcpy;
  84.       *string++ = '%';
  85.       while (string < fmtcpy + sizeof fmtcpy - 1)
  86.         {
  87.           *string++ = *fmt;
  88.           if (! (*fmt >= '0' && *fmt <= '9') && *fmt != '-' && *fmt != ' ')
  89.         break;
  90.           fmt++;
  91.         }
  92.       *string = 0;
  93.       /* Get an idea of how much space we might need.  */
  94.       size_bound = atoi (&fmtcpy[1]) + 50;
  95.       /* Make sure we have that much.  */
  96.       if (size_bound > size_allocated)
  97.         {
  98.           if (big_buffer)
  99.         big_buffer = (char *) xrealloc (big_buffer, size_bound);
  100.           else
  101.         big_buffer = (char *) xmalloc (size_bound);
  102.           sprintf_buffer = big_buffer;
  103.           size_allocated = size_bound;
  104.         }
  105.       minlen = 0;
  106.       switch (*fmt++)
  107.         {
  108.         default:
  109.           error ("Invalid format operation %%%c", fmt[-1]);
  110.  
  111. /*        case 'b': */
  112.         case 'd':
  113.         case 'o':
  114.         case 'x':
  115.           if (cnt == nargs)
  116.         error ("Format string wants too many arguments");
  117.           sprintf (sprintf_buffer, fmtcpy, args[cnt++]);
  118.           /* Now copy into final output, truncating as nec.  */
  119.           string = sprintf_buffer;
  120.           goto doit;
  121.  
  122.         case 'f':
  123.         case 'e':
  124.         case 'g':
  125.           {
  126.         union { double d; char *half[2]; } u;
  127.         if (cnt + 1 == nargs)
  128.           error ("Format string wants too many arguments");
  129.         u.half[0] = args[cnt++];
  130.         u.half[1] = args[cnt++];
  131.         sprintf (sprintf_buffer, fmtcpy, u.d);
  132.         /* Now copy into final output, truncating as nec.  */
  133.         string = sprintf_buffer;
  134.         goto doit;
  135.           }
  136.  
  137.         case 'S':
  138.           string[-1] = 's';
  139.         case 's':
  140.           if (cnt == nargs)
  141.         error ("Format string wants too many arguments");
  142.           string = args[cnt++];
  143.           if (fmtcpy[1] != 's')
  144.         minlen = atoi (&fmtcpy[1]);
  145.           /* Copy string into final output, truncating if no room.  */
  146.         doit:
  147.           tem = strlen (string);
  148.           if (minlen > 0)
  149.         {
  150.           while (minlen > tem && bufsize > 0)
  151.             {
  152.               *bufptr++ = ' ';
  153.               bufsize--;
  154.               minlen--;
  155.             }
  156.           minlen = 0;
  157.         }
  158.           if (tem > bufsize)
  159.         tem = bufsize;
  160.           strncpy (bufptr, string, tem);
  161.           bufptr += tem;
  162.           bufsize -= tem;
  163.           if (minlen < 0)
  164.         {
  165.           while (minlen < - tem && bufsize > 0)
  166.             {
  167.               *bufptr++ = ' ';
  168.               bufsize--;
  169.               minlen++;
  170.             }
  171.           minlen = 0;
  172.         }
  173.           continue;
  174.  
  175.         case 'c':
  176.           if (cnt == nargs)
  177.         error ("Format string wants too many arguments");
  178.           *bufptr++ = (int) args[cnt++];
  179.           bufsize--;
  180.           continue;
  181.  
  182.         case '%':
  183.           fmt--;    /* Drop thru and this % will be treated as normal */
  184.         }
  185.     }
  186.       *bufptr++ = *fmt++;    /* Just some characters; Copy 'em */
  187.       bufsize--;
  188.     };
  189.  
  190.   /* If we had to malloc something, free it.  */
  191.   if (big_buffer)
  192.     xfree (big_buffer);
  193.  
  194.   *bufptr = 0;        /* Make sure our string end with a '\0' */
  195.   return bufptr - buffer;
  196. }
  197.